package view
{
	import AIRProject.filemanager.BrowseForDirectory;
	
	import events.MyEvent;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	
	import view.ui.ExportFolderBoard;
	import view.ui.HandleFolderBoard;
	import view.ui.PngBt;
	import view.ui.TitleTextField;
	import view.ui.WarningTextField;

	public class InitView extends Sprite
	{
		private var title:TitleTextField;
		private var handleFolder:HandleFolderBoard;
		private var exportFolder:ExportFolderBoard;
		private var ok_bt:PngBt;
		private var allReady:Boolean=false;
		private var browser:BrowseForDirectory;
		private var warningTxt:WarningTextField;
		private var handleDir:File;
		private var exportDir:File;
		public function InitView()
		{
			super();
			init();
		}
		
		private function init():void
		{
			title=new TitleTextField();
			title.text="选择文件夹";
			this.addChild(title);
			title.x=40;
			
			handleFolder=new HandleFolderBoard();
			this.addChild(handleFolder);
			handleFolder.x=10;
			handleFolder.y=70;
			handleFolder.addEventListener("handleBt",handleBtClick);
			exportFolder=new ExportFolderBoard();
			this.addChild(exportFolder);
			exportFolder.x=10;
			exportFolder.y=handleFolder.y+handleFolder.height+40;
			exportFolder.addEventListener("exportBt",handleBtClick);
			ok_bt=new PngBt("start");
			this.addChild(ok_bt);
			ok_bt.x=exportFolder.x+exportFolder.width-50;
			ok_bt.y=exportFolder.y+exportFolder.height+10;
			ok_bt.visible=false;
			ok_bt.addEventListener(MouseEvent.CLICK,handleOkClick);
			warningTxt=new WarningTextField();
			warningTxt.x=10;
			warningTxt.y=ok_bt.y+20;
			warningTxt.text="如果选择输出的目录里面有相同的文件,将会被覆盖!";
			this.addChild(warningTxt);
			
			this.addEventListener(Event.REMOVED_FROM_STAGE,handleRemovedFromStage);
		}
		
		private function handleOkClick(e:MouseEvent):void
		{
			this.dispatchEvent(new MyEvent("folderChoseReady",[handleDir,exportDir]));
		}
		
		private function handleBtClick(e:MyEvent):void
		{
			
			switch(e.type)
			{
				case "handleBt":
					browser=new BrowseForDirectory("请选择待处理图片文件夹",handleHandleChosed);
					break;
				case "exportBt":
					browser=new BrowseForDirectory("请选择输出文件夹",handleExportChosed);
					break;
			}
		}
		
		private function checkReady():void
		{
			if(handleDir&&exportDir)
			{
				ok_bt.visible=true;
			}
		}
		
		private function handleExportChosed(file:File):void
		{
			if(file)
			{
				exportDir=file;
				exportFolder.url=file.nativePath;
				exportFolder.showPath();
			}
			checkReady();
		}
		
		private function handleHandleChosed(file:File):void
		{
			if(file)
			{
				handleDir=file;
				handleFolder.url=file.nativePath;
				handleFolder.showPath();
			}
			checkReady();
		}
		
		
		private function removeListener():void
		{
			handleFolder.removeEventListener("handleBt",handleBtClick);
			exportFolder.removeEventListener("exportBt",handleBtClick);
			ok_bt.removeEventListener(MouseEvent.CLICK,handleOkClick);
		}
		
		private function handleRemovedFromStage(e:Event):void
		{
			removeListener();
			this.removeChildren();
			this.removeEventListener(Event.REMOVED_FROM_STAGE,handleRemovedFromStage);
		}
		
	}
}